var express = require('express');
var router = express.Router();
var nodemailer = require("nodemailer");
var path = require('path');
var smtpTransport = require('nodemailer-smtp-transport');
var sqlite3 = require('sqlite3').verbose();

/* GET users listing. */
router.get('/', function(req, res, next) {

    //通过username查询数据库得到email地址
    var db=new sqlite3.Database(path.join(__dirname, "../db/userInfo.db3"));
    const username = req.query.username;
    const captcha= req.query.captcha;
    try{
        db.serialize(function() {
            //sqlStr = "CREATE TABLE IF NOT EXISTS TBL_meta (ID varchar2(255), label varchar2(255))"
            sqlStr = "SELECT email from userInfo where userName='"+username+"'";
            db.all(sqlStr, function(err, rows) {
                if(rows[0]==undefined)
                    res.send('0');
                else if (captcha != req.session.captcha) {
                    res.send('1'); //验证码错误
                } 
                else{
                    res.send('2'); //验证成功
                // 验证通过，删除保存的验证码
                    delete req.session.captcha;
                }
            });

      });
    }catch(ex){
        console.log(ex);
    }
});

module.exports = router;